package cn.szzsi._sys;

import cn.szzsi.common.model.Product;
import cn.szzsi.common.model.ProductCategory;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import org.apache.commons.lang3.StringUtils;
import java.util.Date;
import java.util.List;

/**
 * Created by yuanzheng on 2016/9/22.
 * 商户商品分类
 */
public class ProductCategoryController extends Controller {
    private ProductCategory productCategory;
    private String shopId;

    // 商户商品分类列表
    public void list() {
        shopId="-1";
        setAttr("productCategoryTreeList", ProductCategory.dao.getByUid(shopId));
        render("product_category_list.html");
    }


    // 添加
    public void add() {
        shopId="-1";
        setAttr("productCategoryTreeList", ProductCategory.dao.getByUid(shopId));
        setAttr("shopId",shopId);
        render("product_category_input.html");
    }

    // 编辑
    public void edit() {
        shopId="-1";
        String id = getPara("id","");
        if(StrKit.notBlank(id)){
            setAttr("productCategory", ProductCategory.dao.findById(id));
        }
        setAttr("productCategoryTreeList", ProductCategory.dao.getByUid(shopId));
        setAttr("shopId",shopId);
        render("product_category_input.html");
    }
    // 保存
    public void save(){
        shopId="-1";
        productCategory = getModel(ProductCategory.class);
        String parentId = getPara("parentId",null);
        productCategory.set("parent_id", parentId);
        productCategory.set("uid",shopId);
        productCategory.setId(StrKit.getRandomUUID());
        productCategory.setCreateDate(new Date());
        productCategory.save();
        if (StringUtils.isNotEmpty(parentId)) {
            ProductCategory parent = ProductCategory.dao.findById(parentId);
            if (parent != null) {
                String parentPath = parent.getStr("path");
                productCategory.set("path",parentPath + ProductCategory.PATH_SEPARATOR + productCategory.getStr("id"));
            }
        } else {
            productCategory.set("path",productCategory.getStr("id"));
        }
        productCategory.setModifyDate(new Date());
        productCategory.update();
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
    }

    // 更新
    public void update(){
        productCategory = getModel(ProductCategory.class);
        productCategory.setModifyDate(new Date());
        productCategory.update();
        renderJson(Ret.ok("info","修改成功!").set("url","list"));
    }

    // 删除
    public void delete(){
        String id = getPara("id","");
        if (StrKit.notBlank(id)){
            ProductCategory productCategory = ProductCategory.dao.findById(id);
            // 是否存在下级
            List<ProductCategory> childrenProductCategoryList = productCategory.getChildren();
            if (childrenProductCategoryList != null && childrenProductCategoryList.size() > 0) {
                renderJson(Ret.fail("info","此商品分类存在下级分类，删除失败!"));
                return;
            }
            // 是否存在商品
            List<Product> productList = productCategory.getProductList();
            if (productList != null && productList.size() > 0) {
                renderJson(Ret.fail("info","此商品分类下存在商品，删除失败!"));
                return;
            }
            productCategory.delete();
            renderJson(Ret.ok());
        }
    }
}
